<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .adTop {
            background: gray;
            height: 20px;
            line-height: 20px;
        }

        .adClose {
            float: right;
        }

        .main {
            width: 250px;
            height: 250px;
        }

        .main img {
            width: 100%;
            height: 100%;
        }

        .wrap {
            position: fixed;
            width: 250px;
            height: 270px;
            right: 10px;
            bottom: 10px;
            background: aqua;
        }
    </style>
</head>

<body>
    <!-- <div class="wrap">
        <div class="adTop">
            <span class="adTime">距离广告关闭还有10秒</span><span class="adClose">X</span>
        </div>
        <div class="main">
            <img src="./bzsc-dgbhz-300-250-7-5.gif" alt="">
        </div>
    </div> -->

</body>
<script>
    creatAd()
    function creatAd() {
        var timerr, timer//计时器，定时器 默认
        timerr = setTimeout(() => {
            //清除计时器
            clearInterval(timer)
            clearTimeout(timerr)
            //生成各种东西
            var wrap = document.createElement('div')
            wrap.setAttribute('class', 'wrap')
            var adTop = document.createElement('div')
            adTop.setAttribute('class', 'top')
            var adTime = document.createElement('span')
            adTime.setAttribute('class', 'adTime')
            var adClose = document.createElement('span')
            adClose.setAttribute('class', 'adClose')
            adClose.innerHTML = 'X'
            var main = document.createElement('div')
            main.setAttribute('class', 'main')
            var img = document.createElement('img')
            img.setAttribute('src', './bzsc-dgbhz-300-250-7-5.gif')
            wrap.append(adTop, main)
            adTop.append(adTime, adClose)
            main.append(img)
            //生成完了
            adTime.innerHTML = `距离广告关闭还有10秒`
            var num = 10
            timer = setInterval(() => {
                num--
                adTime.innerHTML = `距离广告关闭还有${num} 秒`
                if (num <= 0) {
                    clearAd()
                }
            }, 1000)
            document.body.append(wrap)//在页面里生成这一大串东西
            //点击清除开始下一个计时
            adClose.onclick = function () {
                clearAd()
            }
            //封装清除AD函数
            function clearAd() {
                clearInterval(timer)
                clearTimeout(timerr)
                wrap.remove()
                wrap = null
                //清除完就开始下一个循环啦
                creatAd()
            }
        }, 5000)
    }





</script>

</html>